//交易页面交互
//




//获取当前
function getJsonTop() {
    $.getJSON("/Ajax/getJsonTop?market="+market+"&t=" + Math.random(), function (data) {
        if (data) {
            if (data['info']['new_price']) {
                $('#market_new_price').removeClass('buy');
                $('#market_new_price').removeClass('sell');
                if ($("#market_new_price").html() >= data['info']['new_price']) {
                    $('#market_new_price').addClass('sell');
                }
                if ($("#market_new_price").html() < data['info']['new_price']) {
                    $('#market_new_price').addClass('buy');
                }
                $("#market_new_price").html(data['info']['new_price']);
            }
            if (data['info']['buy_price']) {
                $('#market_buy_price').removeClass('buy');
                $('#market_buy_price').removeClass('sell');
                if ($("#market_buy_price").html() >= data['info']['buy_price']) {
                    $('#market_buy_price').addClass('sell');
                }
                if ($("#market_buy_price").html() < data['info']['buy_price']) {
                    $('#market_buy_price').addClass('buy');
                }
                $("#market_buy_price").html(data['info']['buy_price']);
                $("#sell_best_price").html('￥' + data['info']['buy_price']);
            }
            if (data['info']['sell_price']) {
                $('#market_sell_price').removeClass('buy');
                $('#market_sell_price').removeClass('sell');
                if ($("#market_sell_price").html() >= data['info']['sell_price']) {
                    $('#market_sell_price').addClass('sell');
                }
                if ($("#market_sell_price").html() < data['info']['sell_price']) {
                    $('#market_sell_price').addClass('buy');
                }
                $("#market_sell_price").html(data['info']['sell_price']);
                $("#buy_best_price").html('￥' + data['info']['sell_price']);
            }
            if (data['info']['max_price']) {
                $("#market_max_price").html(data['info']['max_price']);
            }
            if (data['info']['min_price']) {
                $("#market_min_price").html(data['info']['min_price']);
            }
            if (data['info']['volume']) {
                if (data['info']['volume'] > 10000) {
                    data['info']['volume'] = (data['info']['volume'] / 10000).toFixed(2) + "万"
                }
                if (data['info']['volume'] > 100000000) {
                    data['info']['volume'] = (data['info']['volume'] / 100000000).toFixed(2) + "亿"
                }
                $("#market_volume").html(data['info']['volume']);
            }
            if (data['info']['change']) {
                $('#market_change').parent().removeClass('buy');
                $('#market_change').parent().removeClass('sell');
                if (data['info']['change'] >= 0) {
                    $('#market_change').parent().addClass('sell');
                } else {
                    $('#market_change').parent().addClass('buy');
                }
                $("#market_change").html(data['info']['change'] + "%");
            }


            if (data['list']) {
                var list = '';
                for (var i in data['list']) {
                    list += '<li><a href="/Trade/index/market/' + data['list'][i]['name'] + '"> <img src="__UPLOAD__/coin/' + data['list'][i]['img'] + '" style="width: 40px; float: left; margin-right: 5px;"> <span class="all_coin_name"><p>' + data['list'][i]['title'] + '</p> <span id="all_coin_' + data['list'][i]['name'] + '">' + data['list'][i]['new_price'] + '</span></span></a></li>';
                }
                $("#all_coin").html(list);
            }


        }
    });
    setTimeout('getJsonTop()', 5000);
}

//买入
function tradeadd_buy(){
    if(trans_lock){

        layer.msg($('#len_chongfu').val(),{icon : 2 });

        return;
    }

    var price = parseFloat($('#buy_price').val());
    var num = parseFloat($('#buy_num').val());
    var paypassword = $('#buy_paypassword').val();
    if(price == ""|| price == null || isNaN(price) ){

        layer.tips($('#len_price').val(),'#buy_price',{tips : [3,'#D9AD66'] });

        return false;
    }
    if(num == "" || num == null || isNaN(num) ){

        layer.tips($('#len_num').val(),'#buy_num',{tips : [3,'#D9AD66'] });

        return false;
    }
    trans_lock = 1;
    //加载层-风格3
    layer.load(2);

    //此处演示关闭
    setTimeout(function(){
      layer.closeAll('loading');
      trans_lock = 0;
    }, 10000);

    $.post("/Trade/upTrade.html",{price : $('#buy_price').val(),num : $('#buy_num').val(),paypassword : $('#buy_paypassword').val(),market : market,type : 1 },function(data){
        layer.closeAll('loading');
        trans_lock = 0;
        if(data.status == 1){
            
            $("#buy_price").val('');
            $("#buy_num").val('');
            $("#sell_price").val('');
            $("#sell_num").val('');
            $("#buy_paypassword").val('');
            layer.msg(data.info,{icon : 1 });
        }else{
            layer.msg(data.info,{icon : 2 });
        }
    },'json');
}

//卖出
function tradeadd_sell(){
    if(trans_lock){

        layer.msg($('#len_chongfu').val(),{icon : 2 });

        return;
    }
    
    var price = parseFloat($('#sell_price').val());
    var num = parseFloat($('#sell_num').val());
    var paypassword=$('#sell_paypassword').val();
    if(price==""||price == null || isNaN(price)){

        layer.tips($('#len_price').val(),'#sell_price',{tips : [3,'#D9AD66'] });

        return false;
    }
    if(num=="" || num == null || isNaN(num) ){

        layer.tips($('#len_num').val(),'#sell_num',{tips : [3,'#D9AD66'] });


        return false;
    }
    layer.load(2);
    trans_lock = 1;
    //此处演示关闭
    setTimeout(function(){
        layer.closeAll('loading');
        trans_lock = 0;
    }, 10000);

    $.post("/Trade/upTrade.html",{price : $('#sell_price').val(),num : $('#sell_num').val(),paypassword : $('#sell_paypassword').val(),market : market,type : 2 },function(data){
        layer.closeAll('loading');
        trans_lock = 0;
        if(data.status==1){
            $("#buy_price").val('');
            $("#buy_num").val('');
            $("#sell_price").val('');
            $("#sell_num").val('');
            $("#sell_paypassword").val('');
            layer.msg(data.info,{icon : 1 });
        }else{
            layer.msg(data.info,{icon : 2 });
        }
    },'json');
}

function myLayer(str,$dom){
    str = str ? str : '';
    layer.open({
        type: 1
        ,title: false //不显示标题栏
        ,closeBtn: false
        ,area: '300px;'
        ,shade: 0.5
        ,id: 'LAY_layuipro' //设定一个id，防止重复弹出
        ,resize: false
        ,btn: [$('#len_qushezhi').val()]
        ,btnAlign: 'c'
        ,moveType: 1 //拖拽模式，0或者1
        ,content: '<div class="my-layer-content">'+str+'</div>'
        ,success: function(layero,index){
            $(document).unbind();
            $(document).keyup(function(e){
                if(e.keyCode == 13){
                    layer && layer.close(index);
                    location.href = '/User/paypassword.html';
                }
            });
        },
        yes : function(layero){
            location.href = '/User/paypassword.html';
            layer.closeAll();
        }
    });
};



//保存交易密码设置
function tpwdsettingaa(){
    var paypassword=$("#aaapaypassword").val();
    var tpwdsetting=$("input[name='aaatpwdsetting']:checked").val();
    if(paypassword==""||paypassword==null){
        layer.tips($("#len_enter_trade_password").val(),'#paypassword',{tips : 3 });
        return false;
    }
    if(tpwdsetting==""||tpwdsetting==null){
        layer.tips(_error_global.js_pleaseSelectOne,'#tpwdsetting',{tips : 3 });
        return false;
    }
    $.post('/user/uptpwdsetting', {paypassword : paypassword,tpwdsetting : tpwdsetting}, function(d){
        if(d.status) {
            layer.msg($("#len_shezhichenggong1").val(),{icon : 1 });
            window.location.reload();
        } else {
            layer.msg(d.info,{icon : 2 });
        }

    },'json');
}

//撤销
function cancelaa(id){
    $.post("/Trade/chexiao.html",{id : id },function(data){
        if(data.status==1){
            getEntrustAndUsercoin();
            layer.msg(data.info,{icon : 1 });
        }else{
            layer.msg(data.info,{icon : 2 });
        }
    });
}
//获取当前用户的成交记录
function getEntrustAndUsercoin(){
    $.getJSON("/Ajax/getEntrustAndUsercoin?market="+market+"&t="+Math.random(),function(data){
        if(data){
            if(data['entrust']){
                $('#entrust_over').show();
                var list='';
                var cont=data['entrust'].length;
                for(i=0; i<data['entrust'].length; i++){
                    if(data['entrust'][i]['type']==1){
                        list+='<tr title='+_error_global.js_thisPriceSell+'" onclick="autotrust(this,\'buy\',2)"><td  width="180">'+data['entrust'][i]['addtime']+'</td><td class="buy" width="80">'+$('#len_buy901').val()+'</td><td width="250">'+data['entrust'][i]['price']+'</td><td width="250">'+data['entrust'][i]['num']+'</td><td  width="250">'+data['entrust'][i]['deal']+'</td><td width="250" style="color: #b92e21;">'+(data['entrust'][i]['num']-data['entrust'][i]['deal'])+'</td><td width="80"><a style="color: #b92e21;" class="cancelaa" id="'+data['entrust'][i]['id']+'" onclick="cancelaa(\''+data['entrust'][i]['id']+'\')" href="javascript:void(0);">'+$('#len_chexiao1').val()+'</a></td></tr>';
                    }else{
                        list+='<tr title='+_error_global.js_thisPriceBuy+'" onclick="autotrust(this,\'sell\',2)"><td width="180">'+data['entrust'][i]['addtime']+'</td><td class="sell" width="80">'+$('#len_sell002').val()+'</td><td width="250">'+data['entrust'][i]['price']+'</td><td width="250">'+data['entrust'][i]['num']+'</td><td width="250">'+data['entrust'][i]['deal']+'</td><td width="250" style="color: #b92e21;">'+(data['entrust'][i]['num']-data['entrust'][i]['deal'])+'</td><td width="80"><a style="color: #b92e21;" class="cancelaa" id="'+data['entrust'][i]['id']+'" onclick="cancelaa(\''+data['entrust'][i]['id']+'\')" href="javascript:void(0);">'+$('#len_chexiao1').val()+'</a></td></tr>';
                    }
                }
                if(cont==10){
                    list+='<tr><td style="text_align:center;" colspan="7"><a href="/Finance/mywt" style="color: #2674FF;">'+_error_global.js_moreEntrust+'</a>&nbsp;&nbsp;</td></tr>';
                }
                $('#entrustlist').html(list);
            }else{
                $('#entrust_over').hide();
            }

            if(data['usercoin']){
                if(data['usercoin']['cny']){
                    $("#my_rmb").html(data['usercoin']['cny']);
                }else{
                    $("#my_rmb").html('0.00');
                }

                if(data['usercoin']['cnyd']){
                    $("#my_rmbd").html(data['usercoin']['cnyd']);
                }else{
                    $("#my_rmbd").html('0.00');
                }

                if(data['usercoin']['xnb']){
                    $("#my_xnb,#sell_max").html(data['usercoin']['xnb']);
                }else{
                    $("#my_xnb,#sell_max").html('0.00');
                }

                if(data['usercoin']['xnbd']){
                    $("#my_xnbd").html(data['usercoin']['xnbd']);
                }else{
                    $("#my_xnbd").html('0.00');
                }
            }

        }
    });
    $.getJSON("/Ajax/allfinance?t="+Math.random(),function(data){
        
        $('#user_finance').html(data);
    });
    
    
    setTimeout('getEntrustAndUsercoin()',5000);
}

// 自动填价格
function autotrust(_this,type,cq){
    
    if(type=='sell'){
        $('#buy_price').val($(_this).children().eq(cq).html()).css({'color' : '#333','font_size' : '14px' });
        if($("#my_rmb").html()>0){
            $("#buy_max").html(($("#my_rmb").html()/$('#buy_price').val(),market_round_num));
        }
        if($('#buy_num').val()){
            $("#buy_mum").html(($('#buy_num').val()*$('#buy_price').val()).toFixed(8).toString().replace(/[0]*$/,'').replace(/[\.]*$/,''));
        }
        
    }
    if(type=='buy'){
        $('#sell_price').val($(_this).children().eq(cq).html()).css({'color' : '#333','fontSize' : '14px' });
        if($("#my_xnb").html()>0){
            $("#sell_max").html($("#my_xnb").html());
        }
        if($('#sell_num').val()){
            $("#sell_mum").html(($('#sell_num').val()*$('#sell_price').val()).toFixed(8).toString().replace(/[0]*$/,'').replace(/[\.]*$/,''));
        }
    }
     
}